package com.telecomyt.jwportal.utils;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;

import androidx.appcompat.widget.AppCompatTextView;

import com.telecomyt.jwportal.R;

/**
 * @author created by chengDa
 * @time 2020/8/27  9:56
 * @description
 */
public class RunTextView extends AppCompatTextView {
    private boolean isAnimStart;
    private int duration = 3000;

    public RunTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
        isAnimStart = a.getBoolean(R.styleable.MyTextView_isAnimStart, false);
    }

    /**
     * 开始动画
     */
    public void runTextAnimation(int number) {
        if (number > 0) {
            if (isAnimStart) {
                final ValueAnimator valueAnimator = ValueAnimator.ofInt(0, number);//从0开始，number是你要传的数据
                valueAnimator.setDuration(duration);
                valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        setText(valueAnimator.getAnimatedValue().toString());
                    }
                });
                valueAnimator.start();
            } else {
                setText(number + "");
            }
        }


    }
}